package com.opennews.openplatform.familyexpenseapi.controller

import com.opennews.openplatform.familyexpenseapi.dto.journey.CreateJourneyDto
import com.opennews.openplatform.familyexpenseapi.dto.journey.UpdateJourneyDto
import com.opennews.openplatform.familyexpenseapi.service.JourneyService
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse
import com.opennews.openplatform.myspringbootcore.dto.IdentityDto
import com.opennews.openplatform.myspringbootcore.util.getSuccessResponse
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor
import jakarta.validation.Valid
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/journey")
class JourneyController(
    private val journeyService: JourneyService,
    private val requestIdentityExtractor: RequestIdentityExtractor,
) {
    @GetMapping("/query-list-by-account-group")
    fun queryListByAccountGroup(
        @RequestParam pageSize: Int,
        @RequestParam pageIndex: Int,
    ): ResponseEntity<ApiResponse<List<Map<String, Any>>>> {
        val accountGroupId = requestIdentityExtractor.getClaims().accountGroupId.asString()
        val result = journeyService.queryListByAccountGroup(accountGroupId, pageSize, pageIndex)

        return getSuccessResponse(result)
    }

    @PostMapping("/add")
    fun add(@Valid @RequestBody data: CreateJourneyDto): ResponseEntity<ApiResponse<Any?>> {
        val accountGroupId = requestIdentityExtractor.getClaims().accountGroupId.asString()
        journeyService.add(accountGroupId, data)

        return getSuccessResponse()
    }

    @PostMapping("/update")
    fun update(@Valid @RequestBody data: UpdateJourneyDto): ResponseEntity<ApiResponse<Any?>> {
        val accountGroupId = requestIdentityExtractor.getClaims().accountGroupId.asString()
        journeyService.update(accountGroupId, data)

        return getSuccessResponse()
    }

    @PostMapping("/delete")
    fun delete(@Valid @RequestBody data: IdentityDto): ResponseEntity<ApiResponse<Any?>> {
        val id = data.id.asString()
        val accountGroupId = requestIdentityExtractor.getClaims().accountGroupId.asString()
        val result = journeyService.delete(accountGroupId, id)

        return getSuccessResponse(result)
    }
}